package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.BlessWord;
import com.kgd.news.entity.FrequentlyQuestion;
import com.kgd.news.entity.News;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.DateUtils;
import com.kgd.news.utils.StringUtilsWx;
import com.kgd.news.utils.cache.DbCacheFindUtls;
import com.kgd.news.wxpay.util.StringUtil;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/9/26
 * @Time: 下午11:12queryForm
 */
public class BlessWordController extends BaseController {
//获取祝福语时要缓存的
    @Override
    public void list() {
        String word = getPara("word");
        String type = "bless_word";
        String cid = getPara("cid");
        String sql = "from bless_word where 1 = 1";
        if (!StringUtilsWx.isEmpty(word)) {
            sql += " and word like '%" + word + "%'";
            type += word;
        }
//这个要加缓存的
        if (!StringUtilsWx.isEmpty(cid)) {
            if ("-1".equals(cid)) {
                sql += " and festivaltype is null" ;
            } else {
                sql += " and festivaltype =" + cid;

            }
            type += cid;
        }
        sql += " order by id desc";
//        Page<BlessWord> data=  CacheKit.get("bless_word", type);//type可以作为缓存的key的吧。
//        if (data == null) {
            Page<BlessWord>   data = BlessWord.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
//           CacheKit.put("bless_word", type, data);
//        }
            setAttr("data", data);
            render("blessword_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        BlessWord dict = BlessWord.dao.findById(id);
        setAttr("blessword", dict);
        render("blessword_edit.html");
    }

    @Override
    public void save() {

        UploadFile uploadFile = getFile("img");
        String filePath = "";
        if (uploadFile != null && !StringUtilsWx.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        BlessWord sd = getModel(BlessWord.class, "blessword");
        String word=sd.getWord();
//        sd有可能为空吗
        int festivalType = sd.getFestivaltype();

        if(sd != null && sd.getId() != null){
//            String[] wordArray = word.split("\n");
//            if (wordArray.length > 1) {
//            } else {
//
//            }
            sd.update();
        }else{
            if (!StringUtil.isEmpty(word)) {
                String[] wordArray = word.split("\n");
                if (wordArray.length > 1) {
                    for (int i = 0; i < wordArray.length; i++) {
                        String wordSingle = wordArray[i];
                        if (!StringUtil.isEmpty(wordSingle)) {
                            BlessWord blessWord = new BlessWord();
                            blessWord.setWord(wordSingle);
                            blessWord.setFestivaltype(festivalType);
                            blessWord.save();
                        }
                    }
                } else {
                    sd.save();
                }
            }
        }
        DbCacheFindUtls.removeOneBlessWord(festivalType+"");
        CacheKit.removeAll("blessword");
        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from bless_word where id in (" + ids + ")");
        CacheKit.removeAll("blessword");
        list();
    }

}
